/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2016 - 2021 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */

package net.ccbluex.liquidbounce.features.module.modules.exploit

import net.ccbluex.liquidbounce.event.PacketEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.event.repeatable
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.utils.client.asText
import net.ccbluex.liquidbounce.utils.client.chat
import net.minecraft.network.Packet
import net.minecraft.network.packet.c2s.play.KeepAliveC2SPacket
import net.minecraft.text.TranslatableText
import net.minecraft.util.Formatting

/**
 * NoPitchLimit module
 *
 * Spoofs your ping to a specified value.
 */

object ModulePingSpoof : Module("PingSpoof", Category.EXPLOIT) {

    private val delay by intRange("Delay", 500..1000, 0..25000)

    private val packets = hashMapOf<Packet<*>, Long>()

    override fun disable() {
        packets.clear()
    }

    val packetHandler = handler<PacketEvent> { event ->
        val packet = event.packet

        if (player.isDead) {
            return@handler
        }

        if (packet is KeepAliveC2SPacket && !packets.containsKey(packet)) {
            event.cancelEvent()
            synchronized(packets) {
                packets[packet] = System.currentTimeMillis() + delay.random()
            }
        }
    }

    val repeatable = repeatable {
        if (mc.isIntegratedServerRunning) {
            chat(
                "${TranslatableText("liquidbounce.generic.error")}: ".asText()
                    .styled { it.withColor(Formatting.RED).withBold(true) },
                message(
                    "noSingleplayer",
                    "'PingSpoof'".asText().styled {
                        it.withColor(Formatting.RED).withBold(true)
                    }
                ).styled { it.withColor(Formatting.GREEN) }
            )
            enabled = false
        }

        var filtered: List<Packet<*>>

        synchronized(packets) {
            filtered = packets.entries.filter {
                it.value <= System.currentTimeMillis()
            }.map { it.key }
        }
        for (packet in filtered) {
            network.sendPacket(packet)
            packets.remove(packet)
        }
    }
}
